/*******************************************************************************
 * @project: fant
 * @package: com.acconsys.ant.tasks
 * @file: CopyDirTask.java
 * @author: bruce
 * @created: 2010-11-10
 * @purpose:
 * 
 * @version: 1.0
 * 
 * Revision History at the end of file.
 * 
 * Copyright 2010 AcconSys All rights reserved.
 ******************************************************************************/

package com.acconsys.ant.tasks;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class CopyDirTask extends Task {

	private String outputEncode;
	private String inputDir;
	private String outputDir;
	
	@Override
	public void execute() throws BuildException {
		try {
			File input = new File(inputDir);
			if(!input.exists() || !input.isDirectory()){
				throw new BuildException("inputDir must be a existed directory");
			}
			File output = new File(outputDir);
			if(!output.exists()){
				output.mkdir();
			}
			if (outputEncode == null || outputEncode.trim().length() == 0) {
				outputEncode = "gb2312";
			}

			String separator = System.getProperty("file.separator");
			File[] files = input.listFiles();
			for (int i = 0; i < files.length; i++) {
				if(files[i].isFile()){
					copyFile(files[i].getAbsolutePath(), outputDir + separator + files[i].getName());
				}
			}
			
			log("[fant:CopyDirTask] " + inputDir);
		} catch (Exception e) {
			throw new BuildException(e.getMessage());
		}
	}
	
	private void copyFile(String inputFile, String outputFile){
		try {
			OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(outputFile), outputEncode);

			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String lineSeperator = System.getProperty("line.separator");
			out.write("-- Copy Time: ");
			out.write(dateFormat.format(new Date()));
			
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), "UTF-8"));

            String line = null;
            while ((line = br.readLine()) != null) {
				out.write(lineSeperator);
				out.write(line);
            }
            br.close();
			out.flush();
			out.close();
		} catch (Exception e) {
			throw new BuildException(e.getMessage());
		}
	}

	public String getOutputEncode() {
		return outputEncode;
	}

	public void setOutputEncode(String outputEncode) {
		this.outputEncode = outputEncode;
	}

	public String getInputDir() {
		return inputDir;
	}

	public void setInputDir(String inputDir) {
		this.inputDir = inputDir;
	}

	public String getOutputDir() {
		return outputDir;
	}

	public void setOutputDir(String outputDir) {
		this.outputDir = outputDir;
	}
	
}



/*******************************************************************************
 * <B>Revision History</B><BR>
 * [type 'revision' and press Alt + / to insert revision block]<BR>
 * 
 * 
 * 
 * Copyright 2010 AcconSys All rights reserved.
 ******************************************************************************/